import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    stu: [
      {
        id: 1,
        name: "张三",
        age: "18",
        gender: "男",
        phone: "15775691111",
      },
      {
        id: 2,
        name: "李四",
        age: "18",
        gender: "男",
        phone: "15775692222",
      },
      {
        id: 3,
        name: "王五",
        age: "18",
        gender: "男",
        phone: "15775693333",
      },
      {
        id: 4,
        name: "何能",
        age: "18",
        gender: "男",
        phone: "15775694444",
      },
    ],
  },

  mutations: {
    changeInfo(state, data) {
      // console.log(state.stu, data);
      state.stu[data.id - 1] = data;
    },
    // 删除一条数据
    del(state, data) {
      state.stu.forEach((ele) => {
        if (ele.id == data) {
          state.stu.splice(data - 1, 1);
        }
      });
    },
    // 增加一条数据
    AddInfo(state, data) {
      // console.log(state.stu, data);
      state.stu.push(data);
    },
  },

  actions: {
    changeInfo({ commit }, data) {
      // console.log(data);
      //同步调用vuex的方法
      commit("changeInfo", data);
    },

    // 删除一条数据
    del({ commit }, data) {
      commit("del", data);
    },
    // 增加一条数据
    AddInfo({ commit }, data) {
      commit("AddInfo", data);
    },
  },

  modules: {},
});
